package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.mydoc

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication

import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.IApiProcessCallback
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.createApiAction
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.doPostRequest
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.doUploadImage
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.risking.RiskingGhanaActivity
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.user.mainProcessFormat
import com.helasasa.maxfunds.lineofcredit.cashloan.utils.imageUtil.ImageConvert
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

class MyDocGhanaViewModel : ViewModel() {

    private val _loadingResult = MutableLiveData<Boolean>()
    val loadingResult: LiveData<Boolean> = _loadingResult

    private val _pageDetailResult = MutableLiveData<MyDocGhanaTypeResult>()
    val pageDetailResult: LiveData<MyDocGhanaTypeResult> = _pageDetailResult

    private val _saveOcrResult = MutableLiveData<MyDocGhanaSaveOcrResult>()
    val saveOcrResult: LiveData<MyDocGhanaSaveOcrResult> = _saveOcrResult

    private val _mainProcessResult = MutableLiveData<MyDocGhanaMainProcessResult>()
    val mainProcessResult: LiveData<MyDocGhanaMainProcessResult> = _mainProcessResult

    private val _nextRouterResult =
        MutableLiveData<MyDocGhanaNextRouterResult<AppCompatActivity>>()
    val nextRouterResult: LiveData<MyDocGhanaNextRouterResult<AppCompatActivity>> =
        _nextRouterResult

    /** 图片位置 */
    var pickPicIndex: Int = 0

    /** 查询主流程接口 */
    fun mainProcess() {
        _loadingResult.value = true

        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiAction()
        action["path"] = "D50PrK3UELaosdHcVCL7A"

        context.doPostRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val applyStatus = obj.optString("applyStatus")
                val hadApply = (applyStatus != "NO")
                _mainProcessResult.value =
                    MyDocGhanaMainProcessResult(success = obj, hadApply = hadApply)
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                _mainProcessResult.value =
                    MyDocGhanaMainProcessResult(error = errMsg)
            }
        })
    }

    /**
     * 拉图片回显
     */
    fun pageDetail() {
        _loadingResult.value = true
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiAction()
        action["path"] = "mpqgjYOnwh5tZFq7tWK2tg"

        context.doPostRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                val obj = jsonObject.optJSONArray("obj") ?: JSONArray()
                var idcardFrontPicUrl = _pageDetailResult.value?.idcardFrontPicUrl
                var idcardFrontPicPath = _pageDetailResult.value?.idcardFrontPicPath
                var idcardFrontPicBitmap = _pageDetailResult.value?.idcardFrontPicBitmap
                var idcardFrontPicCanUpdate = true

                var idcardBackPicUrl = _pageDetailResult.value?.idcardBackPicUrl
                var idcardBackPicPath = _pageDetailResult.value?.idcardBackPicPath
                var idcardBackPicBitmap = _pageDetailResult.value?.idcardBackPicBitmap
                var idcardBackPicCanUpdate = true

                var facePicUrl = _pageDetailResult.value?.facePicUrl
                var facePicPath = _pageDetailResult.value?.facePicPath
                var facePicBitmap = _pageDetailResult.value?.facePicBitmap
                var facePicCanUpdate = true

                var payrollPicUrl = _pageDetailResult.value?.payrollPicUrl
                var payrollPicPath = _pageDetailResult.value?.payrollPicPath
                var payrollPicBitmap = _pageDetailResult.value?.payrollPicBitmap
                var payrollPicCanUpdate = true

                var waterBillsPicUrl = _pageDetailResult.value?.waterBillsPicUrl
                var waterBillsPicPath = _pageDetailResult.value?.waterBillsPicPath
                var waterBillsPicBitmap = _pageDetailResult.value?.waterBillsPicBitmap
                var waterBillsPicCanUpdate = true

                var phoneBillsPicUrl = _pageDetailResult.value?.phoneBillsPicUrl
                var phoneBillsPicPath = _pageDetailResult.value?.phoneBillsPicPath
                var phoneBillsPicBitmap = _pageDetailResult.value?.phoneBillsPicBitmap
                var phoneBillsPicCanUpdate = true

                for (index in 0 until obj.length()) {
                    val item = obj[index] as JSONObject
                    val idcardFrontUrl = item.optString("IDCARD_FRONT")
                    if (idcardFrontUrl.isNotBlank()) {
                        idcardFrontPicUrl = idcardFrontUrl
                        idcardFrontPicCanUpdate = false
                    }

                    val idcardBackUrl = item.optString("IDCARD_BACK")
                    if (idcardBackUrl.isNotBlank()) {
                        idcardBackPicUrl = idcardBackUrl
                        idcardBackPicCanUpdate = false
                    }

                    val faceUrl = item.optString("FACE")
                    if (faceUrl.isNotBlank()) {
                        facePicUrl = faceUrl
                        facePicCanUpdate = false
                    }

                    val payrollUrl = item.optString("PAYROLL")
                    if (payrollUrl.isNotBlank()) {
                        payrollPicUrl = payrollUrl
                        payrollPicCanUpdate = false
                    }

                    val waterBillsUrl = item.optString("WATER_BILLS")
                    if (waterBillsUrl.isNotBlank()) {
                        waterBillsPicUrl = waterBillsUrl
                        waterBillsPicCanUpdate = false
                    }

                    val phoneBillsUrl = item.optString("PHONE_BILLS")
                    if (phoneBillsUrl.isNotBlank()) {
                        phoneBillsPicUrl = phoneBillsUrl
                        phoneBillsPicCanUpdate = false
                    }
                }

                _pageDetailResult.value = MyDocGhanaTypeResult(
                    idcardFrontPicUrl = idcardFrontPicUrl,
                    idcardFrontPicPath = idcardFrontPicPath,
                    idcardFrontPicBitmap = idcardFrontPicBitmap,
                    idcardFrontPicCanUpdate = idcardFrontPicCanUpdate,

                    idcardBackPicUrl = idcardBackPicUrl,
                    idcardBackPicPath = idcardBackPicPath,
                    idcardBackPicBitmap = idcardBackPicBitmap,
                    idcardBackPicCanUpdate = idcardBackPicCanUpdate,

                    facePicUrl = facePicUrl,
                    facePicPath = facePicPath,
                    facePicBitmap = facePicBitmap,
                    facePicCanUpdate = facePicCanUpdate,

                    payrollPicUrl = payrollPicUrl,
                    payrollPicPath = payrollPicPath,
                    payrollPicBitmap = payrollPicBitmap,
                    payrollPicCanUpdate = payrollPicCanUpdate,

                    waterBillsPicUrl = waterBillsPicUrl,
                    waterBillsPicPath = waterBillsPicPath,
                    waterBillsPicBitmap = waterBillsPicBitmap,
                    waterBillsPicCanUpdate = waterBillsPicCanUpdate,

                    phoneBillsPicUrl = phoneBillsPicUrl,
                    phoneBillsPicPath = phoneBillsPicPath,
                    phoneBillsPicBitmap = phoneBillsPicBitmap,
                    phoneBillsPicCanUpdate = phoneBillsPicCanUpdate,

                    success = true
                )
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                _pageDetailResult.value = MyDocGhanaTypeResult(error = errMsg)
            }
        })

    }

    /**
     * 上传图片
     */
    private fun uploadImage(filePath: String, type: Int) {
        _loadingResult.value = true

        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>()
        action["filePath"] = filePath
        action["cardType"] = type.toString()

        context.doUploadImage(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val url = obj.optString("url")
                val cardType = obj.optString("cardType", "0").toInt()
                if (url.isNotBlank()) {
                    uploadImageResult(url, cardType)
                } else {
                    uploadImageResult(null, cardType)
                }
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                uploadImageResult(null, type, errMsg)
            }
        })

    }

    /**
     * 保存ocr信息
     */
    private fun saveImageInfo(
        url: String = "",
        type: Int = pickPicIndex
    ) {
        _loadingResult.value = true

        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiAction()
        action["path"] = "x2CrSxT3xM0oGSWRhP6IQ"
        val photoList = mutableListOf<HashMap<String, String>>()
        val item = HashMap<String, String>()
        item["url"] = url
        item["type"] = when (type) {
            0 -> "IDCARD_FRONT"
            1 -> "IDCARD_BACK"
            2 -> "FACE"
            3 -> "PAYROLL"
            4 -> "WATER_BILLS"
            5 -> "PHONE_BILLS"
            else -> "IDCARD_FRONT"
        }
        photoList.add(item)
        action["photoList"] = photoList

        context.doPostRequest(action, object : IApiProcessCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                saveImageResult(url, type)
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                _mainProcessResult.value =
                    MyDocGhanaMainProcessResult(error = errMsg)
            }
        })

    }

    /** 页面跳转 */
    fun nextRouter(jsonObject: JSONObject) {
        // 判断下一步
        val nextRouterResult = jsonObject.mainProcessFormat()
        if (nextRouterResult.nextRouter != null) {
            _nextRouterResult.value =
                MyDocGhanaNextRouterResult(nextPage = nextRouterResult.nextRouter)
        }
    }

    /**
     * 上传图片回显
     */
    private fun uploadImageResult(url: String?, type: Int = pickPicIndex, error: String? = null) {
        try {
            var idcardFrontPicUrl = _pageDetailResult.value?.idcardFrontPicUrl
            var idcardFrontPicPath = _pageDetailResult.value?.idcardFrontPicPath
            var idcardFrontPicBitmap = _pageDetailResult.value?.idcardFrontPicBitmap
            var idcardFrontPicCanUpdate = _pageDetailResult.value?.idcardFrontPicCanUpdate ?: false

            var idcardBackPicUrl = _pageDetailResult.value?.idcardBackPicUrl
            var idcardBackPicPath = _pageDetailResult.value?.idcardBackPicPath
            var idcardBackPicBitmap = _pageDetailResult.value?.idcardBackPicBitmap
            var idcardBackPicCanUpdate = _pageDetailResult.value?.idcardBackPicCanUpdate ?: false

            var facePicUrl = _pageDetailResult.value?.facePicUrl
            var facePicPath = _pageDetailResult.value?.facePicPath
            var facePicBitmap = _pageDetailResult.value?.facePicBitmap
            var facePicCanUpdate = _pageDetailResult.value?.facePicCanUpdate ?: false

            var payrollPicUrl = _pageDetailResult.value?.payrollPicUrl
            var payrollPicPath = _pageDetailResult.value?.payrollPicPath
            var payrollPicBitmap = _pageDetailResult.value?.payrollPicBitmap
            var payrollPicCanUpdate = _pageDetailResult.value?.payrollPicCanUpdate ?: false

            var waterBillsPicUrl = _pageDetailResult.value?.waterBillsPicUrl
            var waterBillsPicPath = _pageDetailResult.value?.waterBillsPicPath
            var waterBillsPicBitmap = _pageDetailResult.value?.waterBillsPicBitmap
            var waterBillsPicCanUpdate = _pageDetailResult.value?.waterBillsPicCanUpdate ?: false

            var phoneBillsPicUrl = _pageDetailResult.value?.phoneBillsPicUrl
            var phoneBillsPicPath = _pageDetailResult.value?.phoneBillsPicPath
            var phoneBillsPicBitmap = _pageDetailResult.value?.phoneBillsPicBitmap
            var phoneBillsPicCanUpdate = _pageDetailResult.value?.phoneBillsPicCanUpdate ?: false

            when (type) {
                0 -> {
                    idcardFrontPicUrl = url
                    if (url == null) {
                        idcardFrontPicPath = null
                        idcardFrontPicBitmap = null
                    }
                }

                1 -> {
                    idcardBackPicUrl = url
                    if (url == null) {
                        idcardBackPicPath = null
                        idcardBackPicBitmap = null
                    }
                }

                2 -> {
                    facePicUrl = url
                    if (url == null) {
                        facePicPath = null
                        facePicBitmap = null
                    }
                }

                3 -> {
                    payrollPicUrl = url
                    if (url == null) {
                        payrollPicPath = null
                        payrollPicBitmap = null
                    }
                }

                4 -> {
                    waterBillsPicUrl = url
                    if (url == null) {
                        waterBillsPicPath = null
                        waterBillsPicBitmap = null
                    }
                }

                5 -> {
                    phoneBillsPicUrl = url
                    if (url == null) {
                        phoneBillsPicPath = null
                        phoneBillsPicBitmap = null
                    }
                }
            }

            _pageDetailResult.value = MyDocGhanaTypeResult(
                idcardFrontPicUrl = idcardFrontPicUrl,
                idcardFrontPicPath = idcardFrontPicPath,
                idcardFrontPicBitmap = idcardFrontPicBitmap,
                idcardFrontPicCanUpdate = idcardFrontPicCanUpdate,

                idcardBackPicUrl = idcardBackPicUrl,
                idcardBackPicPath = idcardBackPicPath,
                idcardBackPicBitmap = idcardBackPicBitmap,
                idcardBackPicCanUpdate = idcardBackPicCanUpdate,

                facePicUrl = facePicUrl,
                facePicPath = facePicPath,
                facePicBitmap = facePicBitmap,
                facePicCanUpdate = facePicCanUpdate,

                payrollPicUrl = payrollPicUrl,
                payrollPicPath = payrollPicPath,
                payrollPicBitmap = payrollPicBitmap,
                payrollPicCanUpdate = payrollPicCanUpdate,

                waterBillsPicUrl = waterBillsPicUrl,
                waterBillsPicPath = waterBillsPicPath,
                waterBillsPicBitmap = waterBillsPicBitmap,
                waterBillsPicCanUpdate = waterBillsPicCanUpdate,

                phoneBillsPicUrl = phoneBillsPicUrl,
                phoneBillsPicPath = phoneBillsPicPath,
                phoneBillsPicBitmap = phoneBillsPicBitmap,
                phoneBillsPicCanUpdate = phoneBillsPicCanUpdate,

                error = error
            )

            // 保存图片
            if (url != null) {
                saveImageInfo(url = url, type = type)
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 保存图片回显
     */
    private fun saveImageResult(url: String?, type: Int = pickPicIndex, error: String? = null) {
        try {
            var idcardFrontPicUrl = _pageDetailResult.value?.idcardFrontPicUrl
            var idcardFrontPicPath = _pageDetailResult.value?.idcardFrontPicPath
            var idcardFrontPicBitmap = _pageDetailResult.value?.idcardFrontPicBitmap
            var idcardFrontPicCanUpdate = _pageDetailResult.value?.idcardFrontPicCanUpdate ?: false

            var idcardBackPicUrl = _pageDetailResult.value?.idcardBackPicUrl
            var idcardBackPicPath = _pageDetailResult.value?.idcardBackPicPath
            var idcardBackPicBitmap = _pageDetailResult.value?.idcardBackPicBitmap
            var idcardBackPicCanUpdate = _pageDetailResult.value?.idcardBackPicCanUpdate ?: false

            var facePicUrl = _pageDetailResult.value?.facePicUrl
            var facePicPath = _pageDetailResult.value?.facePicPath
            var facePicBitmap = _pageDetailResult.value?.facePicBitmap
            var facePicCanUpdate = _pageDetailResult.value?.facePicCanUpdate ?: false

            var payrollPicUrl = _pageDetailResult.value?.payrollPicUrl
            var payrollPicPath = _pageDetailResult.value?.payrollPicPath
            var payrollPicBitmap = _pageDetailResult.value?.payrollPicBitmap
            var payrollPicCanUpdate = _pageDetailResult.value?.payrollPicCanUpdate ?: false

            var waterBillsPicUrl = _pageDetailResult.value?.waterBillsPicUrl
            var waterBillsPicPath = _pageDetailResult.value?.waterBillsPicPath
            var waterBillsPicBitmap = _pageDetailResult.value?.waterBillsPicBitmap
            var waterBillsPicCanUpdate = _pageDetailResult.value?.waterBillsPicCanUpdate ?: false

            var phoneBillsPicUrl = _pageDetailResult.value?.phoneBillsPicUrl
            var phoneBillsPicPath = _pageDetailResult.value?.phoneBillsPicPath
            var phoneBillsPicBitmap = _pageDetailResult.value?.phoneBillsPicBitmap
            var phoneBillsPicCanUpdate = _pageDetailResult.value?.phoneBillsPicCanUpdate ?: false

            when (type) {
                0 -> {
                    idcardFrontPicUrl = url
                    if (url == null) {
                        idcardFrontPicPath = null
                        idcardFrontPicBitmap = null
                    }
                }

                1 -> {
                    idcardBackPicUrl = url
                    if (url == null) {
                        idcardBackPicPath = null
                        idcardBackPicBitmap = null
                    }
                }

                2 -> {
                    facePicUrl = url
                    if (url == null) {
                        facePicPath = null
                        facePicBitmap = null
                    }
                }

                3 -> {
                    payrollPicUrl = url
                    if (url == null) {
                        payrollPicPath = null
                        payrollPicBitmap = null
                    }
                }

                4 -> {
                    waterBillsPicUrl = url
                    if (url == null) {
                        waterBillsPicPath = null
                        waterBillsPicBitmap = null
                    }
                }

                5 -> {
                    phoneBillsPicUrl = url
                    if (url == null) {
                        phoneBillsPicPath = null
                        phoneBillsPicBitmap = null
                    }
                }
            }

            _pageDetailResult.value = MyDocGhanaTypeResult(
                idcardFrontPicUrl = idcardFrontPicUrl,
                idcardFrontPicPath = idcardFrontPicPath,
                idcardFrontPicBitmap = idcardFrontPicBitmap,
                idcardFrontPicCanUpdate = idcardFrontPicCanUpdate,

                idcardBackPicUrl = idcardBackPicUrl,
                idcardBackPicPath = idcardBackPicPath,
                idcardBackPicBitmap = idcardBackPicBitmap,
                idcardBackPicCanUpdate = idcardBackPicCanUpdate,

                facePicUrl = facePicUrl,
                facePicPath = facePicPath,
                facePicBitmap = facePicBitmap,
                facePicCanUpdate = facePicCanUpdate,

                payrollPicUrl = payrollPicUrl,
                payrollPicPath = payrollPicPath,
                payrollPicBitmap = payrollPicBitmap,
                payrollPicCanUpdate = payrollPicCanUpdate,

                waterBillsPicUrl = waterBillsPicUrl,
                waterBillsPicPath = waterBillsPicPath,
                waterBillsPicBitmap = waterBillsPicBitmap,
                waterBillsPicCanUpdate = waterBillsPicCanUpdate,

                phoneBillsPicUrl = phoneBillsPicUrl,
                phoneBillsPicPath = phoneBillsPicPath,
                phoneBillsPicBitmap = phoneBillsPicBitmap,
                phoneBillsPicCanUpdate = phoneBillsPicCanUpdate,

                error = error
            )

            //checkNextRouter()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 判断所有图片上传完成
     */
    fun checkNextRouter() {
        // 判断如果所有图片都上传完成，跳转下一页
        val idcardFrontPicUrlOk =
            _pageDetailResult.value?.idcardFrontPicUrl?.isNotBlank() == true
        val idcardBackPicUrlOk =
            _pageDetailResult.value?.idcardBackPicUrl?.isNotBlank() == true
        val facePicUrlOk =
            _pageDetailResult.value?.facePicUrl?.isNotBlank() == true
        val payrollPicUrlOk =
            _pageDetailResult.value?.payrollPicUrl?.isNotBlank() == true
        val waterBillsPicUrlOk =
            _pageDetailResult.value?.waterBillsPicUrl?.isNotBlank() == true
        val phoneBillsPicUrlOk =
            _pageDetailResult.value?.phoneBillsPicUrl?.isNotBlank() == true

        if (idcardFrontPicUrlOk and idcardBackPicUrlOk and facePicUrlOk and payrollPicUrlOk and waterBillsPicUrlOk and phoneBillsPicUrlOk) {
            _nextRouterResult.value =
                MyDocGhanaNextRouterResult(nextPage = RiskingGhanaActivity::class.java)
        }
    }

    /**
     * 处理图片uri
     */
    @SuppressLint("Recycle")
    fun handlePickImageResult(context: Context, result: Uri?, type: Int = pickPicIndex) {
        try {
            println(result.toString())
            result?.let {
                val path = imageUriToLocalFile(context, it)
                //压缩图片
                ImageConvert.startConvertImage(File(path)) { localPath, thumpImg ->
                    updateImageResult(localPath, thumpImg, type)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 处理图片path
     */
    fun handleTakePictureResult(result: String?, type: Int = pickPicIndex) {
        try {
            println(result.toString())
            result?.let {
                val localPath = it
                val thumpImg = imagePathToBitmap(localPath)
                updateImageResult(localPath, thumpImg, type)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 更新图片显示
     */
    private fun updateImageResult(localPath: String, thumpImg: Bitmap, type: Int = pickPicIndex) {
        try {
            var idcardFrontPicUrl = _pageDetailResult.value?.idcardFrontPicUrl
            var idcardFrontPicPath = _pageDetailResult.value?.idcardFrontPicPath
            var idcardFrontPicBitmap = _pageDetailResult.value?.idcardFrontPicBitmap
            var idcardFrontPicCanUpdate = _pageDetailResult.value?.idcardFrontPicCanUpdate ?: false

            var idcardBackPicUrl = _pageDetailResult.value?.idcardBackPicUrl
            var idcardBackPicPath = _pageDetailResult.value?.idcardBackPicPath
            var idcardBackPicBitmap = _pageDetailResult.value?.idcardBackPicBitmap
            var idcardBackPicCanUpdate = _pageDetailResult.value?.idcardBackPicCanUpdate ?: false

            var facePicUrl = _pageDetailResult.value?.facePicUrl
            var facePicPath = _pageDetailResult.value?.facePicPath
            var facePicBitmap = _pageDetailResult.value?.facePicBitmap
            var facePicCanUpdate = _pageDetailResult.value?.facePicCanUpdate ?: false

            var payrollPicUrl = _pageDetailResult.value?.payrollPicUrl
            var payrollPicPath = _pageDetailResult.value?.payrollPicPath
            var payrollPicBitmap = _pageDetailResult.value?.payrollPicBitmap
            var payrollPicCanUpdate = _pageDetailResult.value?.payrollPicCanUpdate ?: false

            var waterBillsPicUrl = _pageDetailResult.value?.waterBillsPicUrl
            var waterBillsPicPath = _pageDetailResult.value?.waterBillsPicPath
            var waterBillsPicBitmap = _pageDetailResult.value?.waterBillsPicBitmap
            var waterBillsPicCanUpdate = _pageDetailResult.value?.waterBillsPicCanUpdate ?: false

            var phoneBillsPicUrl = _pageDetailResult.value?.phoneBillsPicUrl
            var phoneBillsPicPath = _pageDetailResult.value?.phoneBillsPicPath
            var phoneBillsPicBitmap = _pageDetailResult.value?.phoneBillsPicBitmap
            var phoneBillsPicCanUpdate = _pageDetailResult.value?.phoneBillsPicCanUpdate ?: false

            when (type) {
                0 -> {
                    idcardFrontPicPath = localPath
                    idcardFrontPicBitmap = thumpImg
                }

                1 -> {
                    idcardBackPicPath = localPath
                    idcardBackPicBitmap = thumpImg
                }

                2 -> {
                    facePicPath = localPath
                    facePicBitmap = thumpImg
                }

                3 -> {
                    payrollPicPath = localPath
                    payrollPicBitmap = thumpImg
                }

                4 -> {
                    waterBillsPicPath = localPath
                    waterBillsPicBitmap = thumpImg
                }

                5 -> {
                    phoneBillsPicPath = localPath
                    phoneBillsPicBitmap = thumpImg
                }
            }

            _pageDetailResult.value = MyDocGhanaTypeResult(
                idcardFrontPicUrl = idcardFrontPicUrl,
                idcardFrontPicPath = idcardFrontPicPath,
                idcardFrontPicBitmap = idcardFrontPicBitmap,
                idcardFrontPicCanUpdate = idcardFrontPicCanUpdate,

                idcardBackPicUrl = idcardBackPicUrl,
                idcardBackPicPath = idcardBackPicPath,
                idcardBackPicBitmap = idcardBackPicBitmap,
                idcardBackPicCanUpdate = idcardBackPicCanUpdate,

                facePicUrl = facePicUrl,
                facePicPath = facePicPath,
                facePicBitmap = facePicBitmap,
                facePicCanUpdate = facePicCanUpdate,

                payrollPicUrl = payrollPicUrl,
                payrollPicPath = payrollPicPath,
                payrollPicBitmap = payrollPicBitmap,
                payrollPicCanUpdate = payrollPicCanUpdate,

                waterBillsPicUrl = waterBillsPicUrl,
                waterBillsPicPath = waterBillsPicPath,
                waterBillsPicBitmap = waterBillsPicBitmap,
                waterBillsPicCanUpdate = waterBillsPicCanUpdate,

                phoneBillsPicUrl = phoneBillsPicUrl,
                phoneBillsPicPath = phoneBillsPicPath,
                phoneBillsPicBitmap = phoneBillsPicBitmap,
                phoneBillsPicCanUpdate = phoneBillsPicCanUpdate
            )

            // 上传图片
            uploadImage(localPath, type)

            //checkNextRouter()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun imageUriToLocalFile(
        context: Context,
        contentURI: Uri
    ): String {
        try {
            println(contentURI.path)

            val imgDir = "${
                ProjectApplication.context.applicationContext.filesDir
            }${File.separator}${ProjectApplication.mProduct}_IAGS"

            val dirFile = File(imgDir)
            if (!dirFile.exists()) {
                dirFile.mkdirs()
            }

            val outFilePath = "$imgDir/${System.currentTimeMillis()}.jpg"
            val imgInputStream: InputStream =
                context.contentResolver.openInputStream(contentURI) ?: return ""
            val outFile = File(outFilePath)
            val buffer = ByteArray(1024)
            var len: Int
            val fos = FileOutputStream(outFile)
            while (imgInputStream.read(buffer).also { len = it } != -1) {
                fos.write(buffer, 0, len)
            }
            fos.flush()
            fos.close()
            imgInputStream.close()
            return outFilePath
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    fun imagePathToBitmap(filePath: String?): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeFile(filePath, options)
    }
}